



import { getSetting, chooseAddress, openSetting, showModal, showToast } from '../../utils/asyncWx.js';



// pages/cart/cart.js
Page({
  data: {
    //收货地址
    address: {},
    //购物车信息
    cart: [],
    //是否都选中
    allChecked: false,
    //总价
    totalPrice: 0,
    // 总数量
    totalNum: 0
  },
  onShow() {
    //获取收货地址
    const address = wx.getStorageSync("address");
    this.setData({
      address
    })
    //获取缓存购物车信息
    const cart = wx.getStorageSync("cart") || [];
    //更新数据总数
    this.setCart(cart)
  },
  //点击收货地址
  async handleChooseAddress() {
    try {
      //获取权限状态
      const res1 = await getSetting()
      const scopeAddress = res1.authSetting['scope.address']
      if (scopeAddress == false) {
        // 诱导用户打开
        await openSetting();
      }
      const address = await chooseAddress()
      address.all = address.provinceName + address.cityName + address.countyName + address.detailInfo
      wx.setStorageSync("address", address);

    } catch (error) {

      console.log(error);
    }
  },
  // 商品选中
  handleItemChange(e) {
    //获取id
    const goods_id = e.currentTarget.dataset.id
    //获取商品对象
    let { cart } = this.data
    //找到被修改的商品对象
    let index = cart.findIndex(v => v.goods_id == goods_id)
    // 取反
    cart[index].checked = !cart[index].checked

    // 重新计算总数 总价值 设置进缓存和data
    this.setCart(cart)

  },
  // 设置购物车状态同时 重新计算底部工具栏的数据 全选 总价 购买数量
  setCart(cart) {

    //是否全选
    const allChecked = cart.length ? cart.every(v => v.checked) : false
    //总价格 总数量
    let totalPrice = 0
    let totalNum = 0
    cart.forEach(v => {
      if (v.checked) {
        totalPrice += v.num * v.goods_price
        totalNum += v.num
      }
    });
    //总集数
    this.setData({
      cart,
      allChecked,
      totalPrice,
      totalNum
    })

    wx.setStorageSync("cart", cart);
  },
  //点击全选
  handleAllCheck() {
    let { cart, allChecked } = this.data
    //修改全选状态
    allChecked = !allChecked
    //循环修改选中状态
    cart.forEach(v => { v.checked = allChecked });
    //修改后的值填充回去并更新 已经包含判断全选
    this.setCart(cart)
  },
  //点击加减
  async handleItemNumEdit(e) {
    // 获取传参
    const { id, operation } = e.currentTarget.dataset
    // 获取购物车数据
    let { cart } = this.data
    // 找到需要修改的索引
    let index = cart.findIndex(v => v.goods_id == id)
    //判断数值为1时在减少
    if (cart[index].num == 1 && parseInt(operation) == -1) {

      const res = await showModal({ title: "你好", content: "是否删除" })
      if (res.confirm) {
        cart.splice(index, 1)
        this.setCart(cart)
      }

    } else {
      // 修改值
      cart[index].num += parseInt(operation)
      this.setCart(cart)
    }

  },
  //结算
  async handlePay() {
    //判断收货地址
    const { address } = this.data
    if (!address.userName) {
      await showToast({ title: "请添加收货地址" })
      return
    }
    //判断有没有商品
    const { totalNum } = this.data
    if(totalNum == 0){
      await showToast({title:"请选购商品"})
      return
    }
    //转跳支付页面
    wx.navigateTo({
      url: '/pages/pay/pay'
    });
      
  }
})